package com.zhi.syc.data_applist.services;

import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.zhi.syc.data_applist.beans.ASContactInfoBean;
import com.zhi.syc.data_applist.logger.ASLogger;
import com.zhi.syc.data_applist.util.ASUtil;

import java.util.ArrayList;
import java.util.List;

public class ASContactInfo {
    public static List<ASContactInfoBean> getContactList(Context paramContext) {
        ArrayList<ASContactInfoBean> arrayList = new ArrayList<>();
        try {
            Cursor cursor = paramContext.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
            if (cursor == null) {
                return arrayList;
            }
            if (cursor.moveToFirst()) {
                int j = cursor.getColumnIndex("_id");
                int k = cursor.getColumnIndex("display_name");
                int t = cursor.getColumnIndex("times_contacted");
                int l = cursor.getColumnIndex("last_time_contacted");
                int u = cursor.getColumnIndex("contact_last_updated_timestamp");

                do {
                    String str1 = ASUtil.safeString(cursor.getString(j));
                    String str2 = ASUtil.safeString(cursor.getString(k));
                    String str3 = ASUtil.safeString(cursor.getString(t));
                    String str4 = ASUtil.safeString(cursor.getString(l));
                    String str5 = ASUtil.safeString(cursor.getString(u));

                    if (cursor.getInt(cursor.getColumnIndex("has_phone_number")) <= 0)
                        continue;
                    Cursor cursor1 = paramContext.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, "contact_id=" + str1, null, null);
                    if (cursor1 == null || !cursor1.moveToFirst())
                        continue;
                    do {
                        String mobile = cursor1.getString(cursor1.getColumnIndex("data1"));
                        if (!TextUtils.isEmpty(mobile)) {
                            //去重
                            mobile = mobile.trim();
                            boolean hadMobile = false;
                            for (int i = 0; i < arrayList.size(); i++) {
                                ASContactInfoBean oriBean = arrayList.get(i);
                                if (oriBean.getMobile().equalsIgnoreCase(mobile)) {
                                    hadMobile = true;
                                    break;
                                }
                            }
                            if (!hadMobile) {
                                ASContactInfoBean contact = new ASContactInfoBean();
                                contact.setName(str2);
                                contact.setMobile(mobile);
                                contact.setLastUpdateTime(str5);
                                contact.setLastTimeContacted(str4);
                                contact.setTimesContacted(str3);
                                arrayList.add(contact);
                            }
                        }
                    } while (cursor1.moveToNext());

                    if (!cursor1.isClosed()) {
                        cursor1.close();
                        cursor1 = null;
                    }
                } while (cursor.moveToNext());
            }

            if (!cursor.isClosed()) {
                cursor.close();
                cursor = null;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return arrayList;
    }

    public static String getListZipString(Context paramContext) {
        String zipString = "";
        try {
            List<ASContactInfoBean> contactInfoBeans = getContactList(paramContext);
            String result = new Gson().toJson(contactInfoBeans).trim();
            ASLogger.d(ASContactInfo.class.getSimpleName(), "result: "+result);
            zipString = ASUtil.stringToGZIP(result);
            contactInfoBeans.clear();
            contactInfoBeans = null;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return zipString;
    }
}
